package com.ds.controller;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ds.common.R;
import com.ds.common.redissonLock.RedissonLock;
import com.ds.common.util.RedisUtils;
import com.ds.event.User;
import com.ds.event.UserEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * 测试树表Controller
 *
 * @author Lion Li
 * @date 2021-07-26
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
//@Tag(name = "素材表")
@Slf4j
public class TStorageController {

    private final ApplicationEventPublisher publisher;

    // private final RocketMQTemplate rocketMQTemplate;

    /**
     * 测试spring事件
     * @param uid 用户id
     */
    @GetMapping("/test/{uid}")
    @RedissonLock(key = "#uid")
    @Transactional(rollbackFor = Exception.class)
    public R<User> method(@PathVariable Integer uid) {

        User sysMenu = new User();
        sysMenu.setId(uid);
        sysMenu.setName("张三");

        RedisUtils.set("test2", sysMenu, 10, TimeUnit.SECONDS);
        User s = RedisUtils.get("test2", User.class);

        // SysMenu menu = menuCache.get(uid);
       // SysMenuPageParam sysMenuPageParam = BeanUtil.copyProperties(sysMenu, SysMenuPageParam.class);
        log.info(JSONObject.toJSONString(sysMenu) + "12333");
        publisher.publishEvent(new UserEvent(this, new User(11, "张三", "武汉市")));
        log.info(JSONObject.toJSONString(s));
        log.error(JSONObject.toJSONString(s));

        return R.ok(sysMenu);

    }


    //@GetMapping("/send")
    //public R<String> method() {
    //
    //    List<Message> list = new ArrayList<>();
    //    for (int i = 0; i < 100; i++) {
    //        list.add(MessageBuilder.withPayload("普通消息" + i).build());
    //    }
    //
    //
    //    rocketMQTemplate.syncSend("test", list);
    //    //SendResult sendResult1 = rocketMQTemplate.syncSend("test", "this is a template sync message");
    //    //Message<String> msg = MessageBuilder.withPayload("延时消息测试").build();
    //    //SendResult sendResult2 = rocketMQTemplate.syncSend("test", msg, 100, 3);
    //    return R.ok();
    //
    //}


    @PostMapping("/insert")
    public R<User> insert(@Validated @RequestBody User user) {

        System.out.println(JSONUtil.toJsonStr(user));
        return R.ok(user);

    }

    @PostMapping("/update")
    public R<User> update(@Validated @RequestBody User user) {


        System.out.println(JSONUtil.toJsonStr(user));
        return R.ok(user);

    }


}

